<template>

	<view>
		<!-- <div class="drag">打开同学录</div> -->

		<div v-if='false'>
			<button class="cu-btn round bg-primary drag" style="background-color: #F06292;" type="primary" open-type="getUserInfo"
			 @getuserinfo="mpGetUserInfo">
				<text style="margin-left: 100rpx; margin-right: 100rpx;">授权</text>
			</button>
		</div>


		<view>

			<div>{{myBookInfo.bookName}}</div>

			<div class="flex justify-center" style="width:100%; margin-top:800rpx;">
				<button class="cu-btn round bg-primary margin-xl" style="background-color: #F06292;" type="primary" @click="openBook">
					<text style="margin-left: 100rpx; margin-right: 100rpx;">打开</text>
				</button>
			</div>

		</view>

	</view>

</template>

<script>
	import helper from '../../common/helper.js';
	import Bus from '../../common/bus.vue';
	export default {
		data() {
			return {

				userInfo: {},
				myBookInfo: {},
				writeBookId: '5',
				writeBookInfo: {},
				empty: false,


				tmpUserInfo: {
					"userId": 7,
					"platform": "qq-ytxl",
					"username": null,
					"mobile": null,
					"password": null,
					"createTime": "2019-06-20 18:13:24",
					"wxMiniOpenId": "og_b84_rs30jMqiZ0AjTFpKkuED0",
					"nickName": "La Paisible",
					"userStatus": null,
					"avatar": "https://wx.qlogo.cn/mmopen/vi_32/KB4590biaILdpcMrAaBYKFf9PLF1x4EibovaHwibmibV3z0xtqqJ6kAA9fmFL6vicZGECc5rJAiayD2Ip9clibOvMeTnA/132",
					"sex": null,
					"weight": 1,
					"avrgMenstruationDuration": null,
					"avraMenstruationInterval": null,
					"smsAlert": null,
					"emailAlert": null,
					"alertFrontDays": null,
					"alertTime": "2020-03-02 17:41:56.0",
					"email": null
				}

			}
		},




		onLoad: function() {

			let that = this;
			// todo 测试数据
			uni.setStorage({
				key: 'userInfo',
				data: this.tmpUserInfo,
				success: function() {
					console.log('save userInfo success');
				}
			});


			uni.getStorage({
				key: 'userInfo',
				success: function(res) {
					console.log('index getStorage', res);
					if (res.errMsg === 'getStorage:ok' && res.data != null && res.data != '') {
						that.userInfo = res.data;
						if (that.userInfo != null && that.userInfo.userId != null) {
							that.getBookInfoByUserId(that.userInfo.userId);
						}
					} else {
						// that.showModal();
						// that.hasUserInfo = false;
					}

				},
				fail: function() {
					console.log('getStorage fail');
					// that.showModal();
					// that.hasUserInfo = false;
				}
			});


			//todo
			if (that.writeBookId != null && that.writeBookId != '') {
				that.getBookInfo(that.writeBookId, 'writeBookInfo');
			}


		},

		onShow: function() {
			// console.log('index onShow');
			// let that = this;
			// 查看是否授权
			// storeConfig.removeStore('token')
			// let token = storeConfig.getStore('token')
			// uni.getStorage({
			// 	key: 'recordList',
			// 	success: function(res) {
			// 		console.log('index getStorage', res);
			// 		if (res.errMsg === 'getStorage:ok' && res.data != null && res.data != '') {
			// 			that.recordList = res.data;
			// 		} else {
			// 			// that.showModal();
			// 			// that.hasUserInfo = false;
			// 		}

			// 	},
			// 	fail: function() {
			// 		console.log('getStorage fail');
			// 		// that.showModal();
			// 		// that.hasUserInfo = false;
			// 	}
			// });

		},
		methods: {


			/**
			 * 添加记录
			 */
			openBook(e) {

				//在起始页面跳转到test.vue页面并传递参数
				getApp().globalData.from = 'write';
				getApp().globalData.writeBookInfo = this.writeBookInfo;
				uni.navigateTo({
					url: './write/write?from=toWrite'
				});
				
				
				
			},




			/**
			 * 获取book信息
			 */
			getBookInfo(bookId, changval) {

				let that = this;


				let playod = {

				};

				uni.showLoading({
					title: "加载中"
				});
				uni.request({
					url: helper.api + '/appTxlBookAndAnswer/open/getBookInfoByBookId?bookId=' + bookId,
					method: 'get',
					data: playod,
					header: {
						'Content-Type': 'application/json' //自定义请求头信息
					},

					success: (res) => {
						console.log(res.data);
						uni.hideLoading();
						if (res.data.code === 0) {
							that.$data[changval] = res.data.data;

							// todo 刷新数据
						} else {
							uni.showModal({
								title: '请求失败！',
								content: '错误原因:' + res.data.msg,
								showCancel: false
							});
							return;
						}
					},
					fail: (err) => {
						uni.hideLoading();
						uni.showModal({
							title: '请求失败！',
							content: '错误原因:' + err,
							showCancel: false
						});
					}
				});

			},
			/**
			 * 获取book信息
			 */
			getBookInfoByUserId(userId) {

				let that = this;


				let playod = {

				};

				uni.showLoading({
					title: "加载中"
				});
				uni.request({
					url: helper.api + '/appTxlBookAndAnswer/open/getBookInfoByUserId?userId=' + userId,
					method: 'get',
					data: playod,
					header: {
						'Content-Type': 'application/json' //自定义请求头信息
					},

					success: (res) => {
						console.log(res.data);
						uni.hideLoading();
						if (res.data.code === 0) {
							that.myBookInfo = res.data.data;

							// todo 刷新数据
						} else {
							uni.showModal({
								title: '请求失败！',
								content: '错误原因:' + res.data.msg,
								showCancel: false
							});
							return;
						}
					},
					fail: (err) => {
						uni.hideLoading();
						uni.showModal({
							title: '请求失败！',
							content: '错误原因:' + err,
							showCancel: false
						});
					}
				});

			},



			/**
			 * 提交
			 */
			submit() {

				let that = this;

				that.setFormData();

				let playod = {
					bookId: that.writeBookInfo.bookId,
					bookName: that.writeBookInfo.bookName,
					answerUserId: that.userInfo.userId,
					answerUserName: that.userInfo.userId,
					type: 'open',
					answerItemVOMap: that.answerVOMap,
				};

				uni.showLoading({
					title: "加载中"
				});
				uni.request({
					url: helper.api + '/appTxlBookAndAnswer/open/addOrUpdateAnswer', //仅为示例，并非真实接口地址。
					method: 'post',
					data: playod,
					header: {
						'Content-Type': 'application/json' //自定义请求头信息
					},

					success: (res) => {
						console.log(res.data);
						uni.hideLoading();
						if (res.data.code === 0) {
							that.recordList = res.data.data;

							// todo 刷新数据
						} else {
							uni.showModal({
								title: '请求失败！',
								content: '错误原因:' + res.data.msg,
								showCancel: false
							});
							return;
						}
					},
					fail: (err) => {
						uni.hideLoading();
						uni.showModal({
							title: '请求失败！',
							content: '错误原因:' + err,
							showCancel: false
						});
					}
				});

			}




		},
		computed: {

		}
	}
	// var todolist1 = new Vue({

	// });
</script>

<style>
	/*背景模糊*/
	.bg {
		width: 100%;
		height: 100vh;
		/* position: relative; */
		/* background: url("../../static/img/bg_20200120164709.png") no-repeat fixed; */
		background-image: linear-gradient(45deg, pink, skyblue);
		padding: 1px;
		/* box-sizing: border-box;
		z-index: 1; */
	}

	.bg:after {
		content: "";
		width: 100%;
		height: 100%;
		position: absolute;
		left: 0;
		top: 0;
		background: inherit;
		filter: blur(2px);
		z-index: 2;
	}

	.drag {
		position: absolute;
		left: 50%;
		top: 50%;
		transform: translate(-50%, -50%);
		/*  width:200px;
	    height:200px; */
		text-align: center;
		color: white;

		z-index: 11;
	}

	.cu-form-group .title {
		min-width: calc(4em + 15px);
	}

	.radio-parent {
		flex-wrap: wrap;
	}
</style>
